"""
【请求链】
责任链模式(Chain of Responsibility Pattern)
将请求的发送者和接收者解耦，使得多个对象都有机会处理这个请求。这个模式将这些对象组成一条链，并沿着该链传递该请求，直到有一个对象处理它为止。
"""
from abc import ABC, abstractmethod


class Handler(ABC):
    def __init__(self, successor=None):
        self.successor = successor

    def handle_request(self, request):
        if self.can_handle_request(request):
            self.process_request(request)
        elif self.successor is not None:
            self.successor.handle_request(request)

    @abstractmethod
    def can_handle_request(self, request):
        pass

    @abstractmethod
    def process_request(self, request):
        pass


class ConcreteHandlerA(Handler):
    def can_handle_request(self, request):
        return request >= 0 and request < 3

    def process_request(self, request):
        print(f"Request {request} handled by HandlerA")


class ConcreteHandlerB(Handler):
    def can_handle_request(self, request):
        return request >= 3 and request < 6

    def process_request(self, request):
        print(f"Request {request} handled by HandlerB")


class ConcreteHandlerC(Handler):
    def can_handle_request(self, request):
        return request >= 6 and request < 9

    def process_request(self, request):
        print(f"Request {request} handled by HandlerC")


handler_a = ConcreteHandlerA()
handler_b = ConcreteHandlerB()
handler_c = ConcreteHandlerC()

handler_a.successor = handler_b
handler_b.successor = handler_c

for request in range(1, 10):
    handler_a.handle_request(request)
"""
Request 1 handled by HandlerA
Request 2 handled by HandlerA
Request 3 handled by HandlerB
Request 4 handled by HandlerB
Request 5 handled by HandlerB
Request 6 handled by HandlerC
Request 7 handled by HandlerC
Request 8 handled by HandlerC
"""
